//
//  JSQEmoticonView.m
//  SkyHospital
//
//  Created by C_HAO on 15/4/22.
//  Copyright (c) 2015年 GrayWang. All rights reserved.
//

#import "JSQEmoticonView.h"
#import "JSQMessageTools.h"
#import "JSQEmoticonCollectionViewCell.h"
#import "UIImage+JSQMessages.h"

#define EMOTICON_COUNTS          74
#define EMOTICON_LINE            3

@interface JSQEmoticonView () <UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UIScrollView *emoticonScrollView;

@property (nonatomic, strong) UIPageControl *emoticonPageControl;

@property (nonatomic, assign) NSInteger num;

@property (nonatomic, strong) UIView *contentView;

@end

@implementation JSQEmoticonView

- (instancetype)init {
    self = [super init];
    if (self) {
        _num = floor((CGRectGetWidth([UIScreen mainScreen].bounds))/44);
        [self initWithScrollView];
        [self initWithPageControl];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    _num = floor((CGRectGetWidth([UIScreen mainScreen].bounds))/44);
    [self initWithScrollView];
    [self initWithPageControl];
}

- (void)initWithScrollView {
    _emoticonScrollView = [[UIScrollView alloc] init];
    _emoticonScrollView.pagingEnabled = YES;
    _emoticonScrollView.delegate = self;
    _emoticonScrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _emoticonScrollView.showsHorizontalScrollIndicator = NO;
    _emoticonScrollView.showsVerticalScrollIndicator = NO;

    [self addSubview:_emoticonScrollView];

    _emoticonScrollView.translatesAutoresizingMaskIntoConstraints = NO;

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonScrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonScrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonScrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonScrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];



    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor clearColor];

    [_emoticonScrollView addSubview:contentView];

    _contentView = contentView;

    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    NSInteger pag = EMOTICON_COUNTS/(EMOTICON_LINE * _num) + 1;

    [_emoticonScrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_emoticonScrollView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [_emoticonScrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_emoticonScrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [_emoticonScrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_emoticonScrollView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
    [_emoticonScrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_emoticonScrollView attribute:NSLayoutAttributeRight multiplier:1 constant:0]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:pag constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];


    for (int i = 0; i < pag; i++) {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];

        collectionView.tag = i;
        collectionView.backgroundColor = [UIColor clearColor];

        [collectionView registerNib:[UINib nibWithNibName:@"JSQEmoticonCollectionViewCell" bundle:[NSBundle bundleForClass:[JSQEmoticonCollectionViewCell class]]] forCellWithReuseIdentifier:@"JSQEmoticonCollectionViewCell"];

        collectionView.delegate = self;
        collectionView.dataSource = self;

        [contentView addSubview:collectionView];

        collectionView.translatesAutoresizingMaskIntoConstraints = NO;

        [contentView addConstraint:[NSLayoutConstraint constraintWithItem:collectionView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:CGRectGetWidth([UIScreen mainScreen].bounds) * i]];
        [contentView addConstraint:[NSLayoutConstraint constraintWithItem:collectionView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:CGRectGetWidth([UIScreen mainScreen].bounds)]];


        [contentView addConstraint:[NSLayoutConstraint constraintWithItem:collectionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
        [contentView addConstraint:[NSLayoutConstraint constraintWithItem:collectionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
    }
}

- (void)initWithPageControl {
    _emoticonPageControl = [[UIPageControl alloc] init];
    [_emoticonPageControl setBackgroundColor:[UIColor clearColor]];
    _emoticonPageControl.numberOfPages = EMOTICON_COUNTS/(EMOTICON_LINE * _num) + 1;
    _emoticonPageControl.currentPage = 0;
    [_emoticonPageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

    [self addSubview:_emoticonPageControl];

    _emoticonPageControl.translatesAutoresizingMaskIntoConstraints = NO;

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonPageControl attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonPageControl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonPageControl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:15]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonPageControl attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:-10]];
}

#pragma mark -
#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    CGPoint offset = aScrollView.contentOffset;
    if (aScrollView == self.emoticonScrollView) {
        self.emoticonPageControl.currentPage = offset.x / CGRectGetWidth([UIScreen mainScreen].bounds);
    }
}

#pragma mark -
#pragma mark UIPageControl ActionEvent

- (void)pageTurn:(UIPageControl *)aPageControl {
    NSInteger whichPage = aPageControl.currentPage;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    if (aPageControl == self.emoticonPageControl) {
        self.emoticonScrollView.contentOffset = CGPointMake(CGRectGetWidth([UIScreen mainScreen].bounds) * whichPage, 0.0F);
    }

    [UIView commitAnimations];
}

//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _num * EMOTICON_LINE;
}

//定义展示的Section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSQEmoticonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JSQEmoticonCollectionViewCell" forIndexPath:indexPath];

    cell.emoticonImageView.image = [UIImage jsq_expression:[NSString stringWithFormat:@"mms_face_%02ld@2x", indexPath.row + 1 + (collectionView.tag * _num * EMOTICON_LINE)]];

    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(44, 44);
}

//定义每个UICollectionView 的 margin
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0f;
}

#pragma mark --UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *imageName = [NSString stringWithFormat:@"mms_face_%02ld", indexPath.row + 1 + (collectionView.tag * _num * EMOTICON_LINE)];
    NSString *face = [[JSQMessageTools expressionConvert] objectForKey:imageName];
    if (face) {
        [self.delegate selectTheFace:face];
    }

}

@end
